package com.lmt.controller;

import com.lmt.entity.User;
import com.lmt.feign.ProviderFeignClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;

/**
 * 用户Controller
 * Date 2018/06/28
 *
 * @author MT-Lin
 */
@SuppressWarnings("ALL")
@Import(FeignClientsConfiguration.class)
@RestController
public class UserController {

    private ProviderFeignClient userFeignClient;
    private ProviderFeignClient adminFeignClient;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public UserController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
        this.userFeignClient = Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("user", "password"))
                .target(ProviderFeignClient.class, "http://provider-with-authorize/");
        this.adminFeignClient = Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("admin", "password"))
                .target(ProviderFeignClient.class, "http://provider-with-authorize/");
    }

    @GetMapping(value = "/userFindUserById")
    public User userFindUserById(@RequestParam(value = "id") String id) {
        logger.warn("consumer-feign-manual->userFindUserById:id={}", id);
        return userFeignClient.findUserById(id);
    }

    @GetMapping(value = "/adminFindUserById")
    public User adminFindUserById(@RequestParam(value = "id") String id) {
        logger.warn("consumer-feign-manual->adminFindUserById:id={}", id);
        return adminFeignClient.findUserById(id);
    }
}
